﻿Imports Publics
Imports System.Windows.Forms
Imports System.Data.Linq


Public Class FrmCapNhatKho

    Private _KhoEdit As DM_Kho
    Public Property KhoEdit() As DM_Kho
        Get
            Return _KhoEdit
        End Get
        Set(ByVal value As DM_Kho)
            _KhoEdit = value
        End Set
    End Property

    Private _IDKho As String
    Public Property IDKho() As String
        Get
            Return _IDKho
        End Get
        Set(ByVal value As String)
            _IDKho = value
        End Set
    End Property

    Private _Action As String
    Public Property Action() As String
        Get
            Return _Action
        End Get
        Set(ByVal value As String)
            _Action = value
        End Set
    End Property

    Private Sub btnThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThoat.ItemClick
        Me.Close()
    End Sub

    Private Sub btnGhiVaDong_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnGhiVaDong.ItemClick
        Try

            Dim strLoi As String = CheckSave()
            If strLoi <> "" Then
                MsgInfo(strLoi)
                Return
            End If

            Select Case Action
                Case vAddAction, vCopyAction
                    Dim objNew As New DM_Kho
                    objNew.ID = CreateID()
                    objNew.MaKho = txtMaKho.Text.Trim
                    objNew.TenKho = txtTenKho.Text.Trim
                    objNew.GhiChu = txtGhiChu.Text.Trim
                    objNew.InUsed = True
                    objNew.EditedOn = dbTV.GetDateTimeServer
                    dbTV.DM_Khos.InsertOnSubmit(objNew)
                Case vEditAction
                    Dim objEdit = (From ds In dbTV.DM_Khos Where ds.InUsed AndAlso ds.ID = KhoEdit.ID Select ds).FirstOrDefault
                    If objEdit IsNot Nothing Then
                        objEdit.MaKho = txtMaKho.Text.Trim
                        objEdit.TenKho = txtTenKho.Text.Trim
                        objEdit.GhiChu = txtGhiChu.Text.Trim
                        objEdit.EditedOn = dbTV.GetDateTimeServer
                    End If
            End Select
            iSubmitChange()

            Me.Close()
        Catch ex As Exception
            MsgInfo(ex.Message)
        End Try
    End Sub

    Private Sub FrmCapNhatKho_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lbPhimTat.Text = GetLabelPhimTatCoBanCapNhatDanhMuc()
        btnGhiVaDong.Caption = BtnSaveAndCloseCaption
        btnThoat.Caption = BtnExitCaption

        Select Case Action
            Case vEditAction, vCopyAction
                If KhoEdit IsNot Nothing Then
                    txtMaKho.Text = KhoEdit.MaKho
                    txtTenKho.Text = KhoEdit.TenKho
                    txtGhiChu.Text = KhoEdit.GhiChu
                End If
        End Select
    End Sub

    Private Function CheckSave() As String
        If String.IsNullOrEmpty(txtMaKho.Text.Trim) Then
            txtMaKho.Focus()
            Return "Bạn chưa nhập mã kho"
        End If
        If String.IsNullOrEmpty(txtTenKho.Text.Trim) Then
            txtTenKho.Focus()
            Return "Bạn chưa nhập tên kho"
        End If
        If Action = vAddAction Or Action = vCopyAction Then
            Dim objKiemTra = (From ds In dbTV.DM_Khos Where ds.InUsed AndAlso ds.MaKho = txtMaKho.Text.Trim Select ds).FirstOrDefault
            If objKiemTra IsNot Nothing Then
                txtMaKho.Focus()
                Return "Trùng mã kho"
            End If
        ElseIf Action = vEditAction And txtMaKho.Text.Trim <> KhoEdit.MaKho Then
            Dim objKiemTra = (From ds In dbTV.DM_Khos Where ds.InUsed AndAlso ds.MaKho = txtMaKho.Text.Trim Select ds).FirstOrDefault
            If objKiemTra IsNot Nothing Then
                txtMaKho.Focus()
                Return "Trùng mã kho"
            End If
        End If
        Return ""
    End Function

    Private Sub FrmCapNhatKho_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Escape Then
            btnThoat_ItemClick(Nothing, Nothing)
        End If
        'Ghi và đóng
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pSAVECLOSE, btnGhiVaDong) Then
            btnGhiVaDong_ItemClick(Nothing, Nothing)
        End If
    End Sub
End Class